package krypto.blinsignature.app;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import krypto.blindsignature.service.SignCreatorService;
import net.miginfocom.swing.MigLayout;

import org.springframework.beans.factory.annotation.Autowired;

public class App {

	private JFrame frmlepyPodpisywak;

	@Autowired
	SignCreatorService service;
	private JTextArea msgField;
	private JTextArea sgnField;
	private JTextArea sgnMsgField;
	private Label label_1;
	private Label label_2;
	private List<BigInteger> signatures;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPane_2;

	/**
	 * Launch the application.
	 */
	public void run() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					frmlepyPodpisywak.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public App() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmlepyPodpisywak = new JFrame();
		frmlepyPodpisywak.setIconImage(Toolkit.getDefaultToolkit().getImage(App.class.getResource("/org/apache/log4j/lf5/viewer/images/channelexplorer_satellite.gif")));
		frmlepyPodpisywak
				.setTitle("Ślepy podpisywak client v. 1.0.0-SNAPSHOT");
		frmlepyPodpisywak.setBounds(100, 100, 597, 460);
		frmlepyPodpisywak.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmlepyPodpisywak.getContentPane().setLayout(
				new MigLayout("", "[grow,left][grow,left]", "[fill][grow,fill][][fill][grow,fill][]"));

		Label label = new Label("Wiadomo\u015B\u0107");
		frmlepyPodpisywak.getContentPane().add(label, "cell 0 0");
		
		scrollPane = new JScrollPane();
		frmlepyPodpisywak.getContentPane().add(scrollPane, "cell 0 1 2 1,grow");

		msgField = new JTextArea();
		scrollPane.setViewportView(msgField);
		msgField.setFont(new Font("Arial", Font.PLAIN, 12));
		msgField.setWrapStyleWord(true);
		msgField.setColumns(10);


		JButton btnNewButton = new JButton("Podpisz wiadomość");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String message = msgField.getText();
				signatures = service.signMessage(message);

				StringBuilder strBuilder = new StringBuilder();
				for (BigInteger sgn : signatures) {
					strBuilder.append(sgn.toString() + "\n");
				}
				sgnField.setText(strBuilder.toString());
			}
		});
		frmlepyPodpisywak.getContentPane().add(btnNewButton,
				"cell 0 2 2 1,alignx center");

		label_1 = new Label("\u015Alepy podpis");
		frmlepyPodpisywak.getContentPane().add(label_1, "cell 0 3");

		label_2 = new Label("Odkodowany podpis");
		frmlepyPodpisywak.getContentPane().add(label_2, "cell 1 3");
		
		scrollPane_1 = new JScrollPane();
		frmlepyPodpisywak.getContentPane().add(scrollPane_1, "cell 0 4,grow");

		sgnField = new JTextArea();
		scrollPane_1.setViewportView(sgnField);
		sgnField.setColumns(10);
		
		scrollPane_2 = new JScrollPane();
		frmlepyPodpisywak.getContentPane().add(scrollPane_2, "cell 1 4,grow");

		sgnMsgField = new JTextArea();
		scrollPane_2.setViewportView(sgnMsgField);
		sgnMsgField.setColumns(10);


		JButton btnNewButton_1 = new JButton("Sprawdź podpis");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sgnMsgField.setText(service.decryptMessageFromSignature(signatures));
			}
		});
		frmlepyPodpisywak.getContentPane().add(btnNewButton_1,
				"cell 0 5 2 1,alignx center");
		sgnField.setLineWrap(true);
		msgField.setLineWrap(true);
		sgnMsgField.setLineWrap(true);
	}

}
